package nl.hr.cmi.opleiding;

import java.text.SimpleDateFormat;
import java.util.List;

import nl.hr.cmi.opleiding.database.DBAgendaItem;
import nl.hr.cmi.opleiding.database.DBAgendaItemDatabaseHelper;
import android.os.Bundle;
import android.util.Log;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TableRow.LayoutParams;
import android.widget.TextView;

import com.j256.ormlite.android.apptools.OrmLiteBaseActivity;
import com.j256.ormlite.dao.RuntimeExceptionDao;

public class Agenda extends OrmLiteBaseActivity<DBAgendaItemDatabaseHelper> {

	private final String TAG = getClass().getSimpleName();
	private TableLayout tl_agenda;
	
	//TODO make this in dutch
	SimpleDateFormat dateFormatter = new SimpleDateFormat("dd MMM");
	
	//TODO this is really bad
	private int pixel;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.agenda);
		
		pixel=this.getWindowManager().getDefaultDisplay().getWidth();

		tl_agenda = (TableLayout) findViewById(R.id.agenda_table);

		RuntimeExceptionDao<DBAgendaItem, Integer> agendaDao = getHelper()
				.getDBAgendaItemDao();

		// query for all of the data objects in the database
		List<DBAgendaItem> list = agendaDao.queryForAll();
		

		for (DBAgendaItem agenda : list) {
			//			Log.e(LOG_TAG,agendal.getTitle());
			
			TableRow tr = new TableRow(getBaseContext());
			tr.setLayoutParams(new TableRow.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
			
			TextView tv_datum = new TextView(getBaseContext());
			tv_datum.setText(dateFormatter.format(agenda.getDate()));
			tv_datum.setLines(3);
			tv_datum.setWidth((int)pixel/6);
			
			
			LinearLayout ll = new LinearLayout(getBaseContext());
			//ll.setLayoutParams(new LinearLayout.LayoutParams(Layou, LayoutParams.WRAP_CONTENT));
			ll.setOrientation(LinearLayout.VERTICAL);
			
			TextView tv_title = new TextView(getBaseContext());
			tv_title.setText(agenda.getTitle());
			
			TextView tv_text = new TextView(getBaseContext());
			tv_text.setText(agenda.getText());
			
			tv_text.setWidth((pixel/6)*5);
			
				
			
			tr.addView(tv_datum);
			ll.addView(tv_title);
			ll.addView(tv_text);
			tr.addView(ll);
			
			tl_agenda.addView(tr);	
				
				
			
			
		}
	}

}
